package com.meichuang.meipian.video.textbitmap;

import android.graphics.Typeface;

import org.json.JSONObject;

/**
 * Created by ken on 17/11/12.
 */

public class TextInfoModel {

    Typeface font;


    public int textureId;// = object.getInt("textureId");
    public String userTextString;// = object.getString("userTextString");
    public String userTextStringResult[];// = object.getString("userTextString");
    public String textureType;// = object.getString("textureType");
    public int textureSizeMode;// = object.getInt("textureSizeMode");
    public int textVerticalDrawAlignmentMode;// = object.getInt("textVerticalDrawAlignmentMode");
    public int textHorizontalDrawAlignmentMode;
    public int textMaxLenght;// = object.getInt("textMaxLenght");
    public int textLayoutMode;// = object.getInt("textLayoutMode");
    public float textFixedResolution[];// = object.getString("textFixedResolution");

    public int textAlignment;// = object.getInt("textAlignment");
    public float textColor[];// = object.getString("textColor");
    public float textBgColor[];// = object.getString("textBgColor");
    public float strokeColor[];// = object.getString("strokeColor");
    public float shadowColor[];// = object.getString("shadowColor");

    public int strokeWidth;// = object.getString("strokeWidth");

    public float shadowOffset[];// = object.getString("shadowOffset");

    public double shadowBlurRadius;// = object.getDouble("shadowBlurRadius");
    public String sceneResourceFolderPath;// = object.getString("sceneResourceFolderPath");
    public boolean isShowTextShadow;// = object.getBoolean("strokeColor");
    public boolean isShowTextBgColor;// = object.getBoolean("isShowTextBgColor");
    public String fontName;// = object.getString("fontName");
    public int fontSize;// = object.getInt("sceneResourceFolderPath");
    public int textDrawingMode;

    public TextInfoModel(){

        strokeColor = new float[4];
        shadowColor = new float[4];
        textColor = new float[4];
        textBgColor = new float[4];
        textFixedResolution = new float[2];
        shadowOffset = new float[2];
    }
    public void valueFromJson(String textjsonInfo){
        try {
            JSONObject object=new JSONObject(textjsonInfo);
            textureId = object.getInt("textureId");
            userTextString = object.getString("userTextString");
            userTextStringResult = userTextString.split("\\n");
            textDrawingMode = object.getInt("textDrawingMode");
            textureType = object.getString("textureType");
            textureSizeMode = object.getInt("textureSizeMode");
            textVerticalDrawAlignmentMode = object.getInt("textVerticalDrawAlignmentMode");
            textHorizontalDrawAlignmentMode = object.getInt("textHorizontalDrawAlignmentMode");
            textMaxLenght = object.getInt("textMaxLenght");
            textLayoutMode = object.getInt("textLayoutMode");
            String  str= object.getString("textFixedResolution");
            if(str != null && str.length()>0) {
                String s[] = str.split(",");
                textFixedResolution[0] = Float.valueOf(s[0]);
                textFixedResolution[1] = Float.valueOf(s[1]);
            }
            str= object.getString("textColor");
            if(str != null && str.length()>0) {
                String s[] = str.split(",");
                textColor[0] = Float.valueOf(s[0]);
                textColor[1] = Float.valueOf(s[1]);
                textColor[2] = Float.valueOf(s[2]);
                textColor[3] = Float.valueOf(s[3]);
            }
            textAlignment = object.getInt("textAlignment");
            str= object.getString("textBgColor");
            if(str != null && str.length()>0){
                String s[] = str.split(",");
                textBgColor[0] = Float.valueOf(s[0]);
                textBgColor[1] = Float.valueOf(s[1]);
                textBgColor[2] = Float.valueOf(s[2]);
                textBgColor[3] = Float.valueOf(s[3]);
            }

            strokeWidth = object.getInt("strokeWidth");
            str= object.getString("strokeColor");
            if(str != null && str.length()>0){
                String s[] = str.split(",");
                strokeColor[0] = Float.valueOf(s[0]);
                strokeColor[1] = Float.valueOf(s[1]);
                strokeColor[2] = Float.valueOf(s[2]);
                strokeColor[3] = Float.valueOf(s[3]);
            }
            str= object.getString("shadowOffset");
            if(str != null && str.length()>0){
                String s[] = str.split(",");
                shadowOffset[0] = Float.valueOf(s[0]);
                shadowOffset[1] = Float.valueOf(s[1]);
            }
            str= object.getString("shadowColor");
            if(str != null && str.length()>0){
                String s[] = str.split(",");
                shadowColor[0] = Float.valueOf(s[0]);
                shadowColor[1] = Float.valueOf(s[1]);
                shadowColor[2] = Float.valueOf(s[2]);
                shadowColor[3] = Float.valueOf(s[3]);
            }
            shadowBlurRadius = object.getDouble("shadowBlurRadius");
            sceneResourceFolderPath = object.getString("sceneResourceFolderPath");
            isShowTextShadow = object.getBoolean("isShowTextShadow");
            isShowTextBgColor = object.getBoolean("isShowTextBgColor");
            fontName = object.getString("fontName");
            if(!object.isNull("fontName_inputpath") && object.getString("fontName_inputpath") != null){//是否有用户自己选择了字体
                fontName = object.getString("fontName_inputpath");
            }
            if(fontName!=null){//这里有问题路径不对 后面再看这个问题
                if(fontName.indexOf("$(currentScenePlistROOT)")>-1){
                    fontName = fontName.replace("$(currentScenePlistROOT)/",sceneResourceFolderPath);
                    fontName = fontName.replace("//","/");
                    try{
                        font = Typeface.createFromFile(fontName);
                    }catch (Exception e){
                        e.printStackTrace();
                    }

                }else{
                    try{
                        font = Typeface.createFromFile(fontName);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    if(font == null){
                        font = Typeface.create(fontName, Typeface.BOLD);
                    }
                }
                if(font == null){
                    font = Typeface.create("宋体", Typeface.BOLD);
                }
            }else{
                font = Typeface.create("宋体", Typeface.BOLD);
            }
            fontSize = object.getInt("fontSize");
        }catch (Exception e){
            e.printStackTrace();
        }


    }
}
